<?php

class SatuanController extends Controller
{
    public $layout='//layouts/column2';
    public $defaultAction = 'admin';

    protected function getIndexList($model) {
        $indexList = array(
            array(
		'name' => 'nama',
		'value' => '($data->nama) ? $data->nama : "-"',
	),            array(
                'htmlOptions' => array('nowrap'=>'nowrap','class'=>'button-column'),
                'class'=>'booster.widgets.TbButtonColumn'
            ),
        );
        return $indexList;
    }

    protected function getViewList($model) {
        $viewList = array(
            array(
	'name' => 'nama',
	'value' => ($model->nama) ? $model->nama : "-",
),        );
        return $viewList;
    }

    /**
    * Displays a particular model.
    * @param integer $id the ID of the model to be displayed
    */
    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }

    public function actionCreate()
    {
        $model=new Satuan;
        $this->performAjaxValidation($model);
        if(isset($_POST['Satuan']))
        {
            $model->attributes=$_POST['Satuan'];
            if($model->save())
                $this->redirect(array($this->defaultAction));
        }
        $this->render('create',array(
            'model'=>$model,
        ));
    }

    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);
        $this->performAjaxValidation($model);
        if(isset($_POST['Satuan']))
        {
            $model->attributes=$_POST['Satuan'];
            if($model->save())
                $this->redirect(array($this->defaultAction));
        }
        $this->render('update',array(
            'model'=>$model,
        ));
    }

    public function actionDelete($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            $this->loadModel($id)->delete();
            if(!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
            }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }

    /**
    * Manages all models.
    */
    public function actionAdmin()
    {
        $model=new Satuan('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Satuan']))
            $model->attributes=$_GET['Satuan'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    public function loadModel($id)
    {
        $model=Satuan::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='satuan-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
